package com.ha.garbage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.ha.garbage.adapter.GarbageListAdapter;
import com.ha.garbage.adapter.NewsListAdapter;
import com.ha.garbage.model.News;
import com.ha.garbage.model.Result;
import com.ha.garbage.model.request.banner.BannerInfo;
import com.ha.garbage.model.request.banner.BannerInfoNet;
import com.ha.garbage.service.BannerService;
import com.ha.garbage.service.NewsService;
import com.ha.garbage.service.userService;
import com.ha.garbage.utils.UserRetrofit;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Url;


public class HomeFragment extends Fragment {
    private Banner banner;
    private  List<BannerInfo> list = new ArrayList<>();
    private  List<BannerInfoNet> list1 = new ArrayList<>();
    private RecyclerView recyclerView;
    private NewsListAdapter newsListAdapter;

    private List<News> newsList  = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化控件
        banner = view.findViewById(R.id.banner);
        recyclerView  = view.findViewById(R.id.rv_news);
     //查询数据
        Retrofit retrofit1 = UserRetrofit.retrofit();
        NewsService newsService = retrofit1.create(NewsService.class);
        Call<Result<List<News>>> news = newsService.getNews();
        news.enqueue(new Callback<Result<List<News>>>() {
            @Override
            public void onResponse(Call<Result<List<News>>> call, Response<Result<List<News>>> response) {
                if(response.isSuccessful()){
                    Result<List<News>> body = response.body();
                    if(body.getCode() == 1){
                        System.out.println("查询成功");
                        List<News> data = body.getData();
                        newsList = data;
                        //初始化适配器
                        newsListAdapter =   new NewsListAdapter(newsList);
                        //设置适配器
                        recyclerView.setAdapter(newsListAdapter);
                    }else {
                        new AlertDialog.Builder(getContext())
                                .setMessage("没有资讯数据")
                                .setPositiveButton("确定",null)
                                .show();
                    }
                }
            }

            @Override
            public void onFailure(Call<Result<List<News>>> call, Throwable t) {

                System.out.println("请求路径为："+call.request().url().toString());
                System.out.println("资讯服务错误");
                t.printStackTrace();
            }
        });


        //查询数据
        Retrofit retrofit = UserRetrofit.retrofit();
        BannerService bannerService = retrofit.create(BannerService.class);
        Call<Result<List<BannerInfoNet>>> call = bannerService.getBanner();
      call.enqueue(new Callback<Result<List<BannerInfoNet>>>() {
          @Override
          public void onResponse(Call<Result<List<BannerInfoNet>>> call, Response<Result<List<BannerInfoNet>>> response) {

              Result<List<BannerInfoNet>> body = response.body();
              if(response.isSuccessful()){
                  if (body.getCode() == 1 ){
                      List<BannerInfoNet> data = body.getData();
                      Iterator<BannerInfoNet> iterator = data.iterator();
                      while (iterator.hasNext()){
                          BannerInfoNet infoNet = iterator.next();
                          list1.add(new BannerInfoNet(infoNet.getImg(),infoNet.getTitle()));
                      }
                  }else {
                      System.out.println("响应出错");
                  }

              }else {
                  System.out.println("请求出错");
              }


          }

          @Override
          public void onFailure(Call<Result<List<BannerInfoNet>>> call, Throwable t) {

              System.out.println("错误原因"+t.toString());
          }
      });


        // 设置adapter
        banner.setAdapter(new BannerImageAdapter<BannerInfoNet>(list1) {
                    @Override
                    public void onBindView(BannerImageHolder holder, BannerInfoNet data, int position, int size) {
                        // 设置数据
                        Glide.with(holder.imageView)
                                .load(data.getImg())
                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                .into(holder.imageView);

                    }
                })
                .addBannerLifecycleObserver(this)
                .setIndicator(new CircleIndicator(requireContext()));
    }
}